<?php
class OrderModel extends Db
{
    protected $_order_list = 'w_order_list'; /*订单列表*/
    protected $_order_info = 'w_order_info'; /*订单信息*/
    protected $_order_conf = 'w_order_conf'; /*订单信息*/
    protected $_order_history = 'w_order_history'; /*操作历史记录*/
    protected $_coupons_history = 'w_coupons_history'; //积分兑换
    protected $_coupons_audit = 'w_coupons_audit'; //日结算
    public function getLastAudit ()
    {
        //统计当前用户账户总额  获取最新的一条记录
        $sql = "select * from $this->_coupons_audit where id=(select MAX(id) FROM w_coupons_audit)";
        return $this->fetch($sql);
    }
    /**
     * 获取最新的积分的积分
     * Enter description here ...
     */
    public function getMaxCoupons ()
    {
        $sql = "select max(id) id from $this->_coupons_history";
        return $this->fetch($sql);
    }
    public function getAudit ()
    {
        return $this->getAll($this->_coupons_audit, null, null, 'id DESC', 20);
    }
    public function getLastIncome ($id)
    {
        $sql = "SELECT sum(coupons) income FROM $this->_coupons_history WHERE payment=0 AND status=8 AND id>$id";
        return $this->fetch($sql);
    }
    /**
     * 新增参数
     * Enter description here ...
     * @param unknown_type $params
     */
    public function addAudit ($params)
    {
        $this->add($this->_coupons_audit, $params);
        return $this->lastInsertId();
    }
    public function getLastSpend ($id, $status)
    {
        $sql = "SELECT sum(coupons) spend FROM $this->_coupons_history WHERE payment=1 AND status=$status AND id>$id";
        return $this->fetch($sql);
    }
    /*获取等待审核的订单*/
    public function getVerifyOrder ()
    {
        $sql = "select a.id,a.address,a.remark,a.addtime,a.orderno,shr,goods_name,b.num,b.coupons,b.coupons_total from w_order_list a left join w_order_info b on a.orderno=b.orderno where  status=1 order by shr";
        return $this->fetchAll($sql);
    }
    public function getSumCouponsByUid ($uid)
    {
        $sql = "SELECT sum(coupons) total FROM w_coupons_history WHERE uid=$uid AND status IN (1,8)";
        return $this->fetch($sql);
    }
    /**
     * 获取历史记录
     * Enter description here ...
     * @param unknown_type $where
     */
    public function getHistory ($where)
    {
        return $this->getAll($this->_order_history, $where);
    }
    public function countOrderHisotry ()
    {
        $sql = "SELECT id FROM $this->_order_history";
        return $this->rowCount($sql);
    }
    /**
     * 获取产品价格
     * Enter description here ...
     * @param unknown_type $where
     */
    public function getGoodsExtendByGid ($where)
    {
        return $this->getAll('w_extend_value', $where);
    }
    public function getGoodsByGid ($where)
    {
        return $this->getOne('w_news_list', $where);
    }
    /**
     * 获取产品信息
     * @param unknown_type $orderno
     */
    public function getGoodsInfo ($orderno)
    {
        $sql = "select a.*,b.thumb from w_order_info a LEFT JOIN w_news_list b ON a.goods_id=b.id WHERE a.orderno='$orderno'";
        return $this->fetchAll($sql);
    }
    /**
     * 添加历史记录
     * Enter description here ...
     * @param unknown_type $params
     */
    public function addHistory ($orderno, $remark, $action)
    {
        $this->add($this->_order_history, 
        array('orderno' => $orderno, 'remark' => $remark, 'action' => $action, 
        'action_time' => time()));
    }
    /**
     * 获取产品全部价格
     * Enter description here ...
     */
    public function getGoodsPrice ()
    {
        $sql = "select a.id,a.cid,a.title,b.`decimal` from w_news_list a left join w_extend_value b on a.id=b.gid where b.key='dljg';";
        return $this->fetchAll($sql);
    }
    /**
     * 获取sno*/
    public function getSno ()
    {
        return $this->getOne($this->_order_conf, array('name' => 'sno'));
    }
    /**
     * 获取订单数量
     * 
     */
    public function getOrderNum ()
    {
        return $this->getOne($this->_order_conf, array('name' => 'num'));
    }
    /**
     * 根据订单号获取产品信息
     * Enter description here ...
     * @param unknown_type $orderno
     */
    public function getGoodsBySNO ($orderno)
    {
        return $this->getAll($this->_order_info, array('orderno' => $orderno));
    }
    /**
     * 修改订单状态
     * Enter description here ...
     * @param unknown_type $v
     * @param unknown_type $where
     */
    public function saveStatus ($v, $where)
    {
        $this->update($this->_order_list, $v, $where);
    }
    /**
     * 修改订单中产品状态
     * Enter description here ...
     * @param unknown_type $v
     * @param unknown_type $where
     */
    public function saveGoodsStatus ($v, $where)
    {
        $this->update($this->_order_info, $v, $where);
    }
    /**
     * 查看评论
     *
     * @param array $where
     * contentId
     */
    public function commentPage ($where = NULL, $start, $end, $type = NULL)
    {
        if (empty($where)) {
            $sql = "SELECT * FROM $this->_order_list ORDER BY  id DESC LIMIT $start,$end";
        } elseif ($type == NULL && ! empty($where)) {
            $where = $this->batchWhere($where);
            $sql = "SELECT * FROM $this->_order_list WHERE $where ORDER BY  id DESC LIMIT $start,$end";
        }
        if ($type == 'status') {
            $sql = "SELECT * FROM $this->_order_list WHERE `status` IN ($where) ORDER BY  id DESC LIMIT $start,$end";
        }
        $rs = $this->fetchAll($sql);
        // $rs = $this->getAll($this->_news_comment, $where, null, 'id DESC',
        // $num);
        return $rs;
    }
    /*单号*/
    public function getOrderByWhere ($where)
    {
        return $this->getAll($this->_order_list, $where);
    }
    /**
     * 获取订单数量
     * Enter description here ...
     * @param unknown_type $key
     * @param unknown_type $where
     */
    public function getCommentNum ($where = NULL)
    {
        $sql = "SELECT count(id) num FROM $this->_order_list";
        $num = $this->fetch($sql);
        return $num['num'];
    }
    /**
     * 添加订单
     *
     * @param unknown_type $params
     */
    public function addOrder ($params)
    {
        return $this->add($this->_order_list, $params);
    }
    /**
     * 添加订单详情
     * @param unknown_type $params
     */
    public function addGoods ($params)
    {
        return $this->add($this->_order_info, $params);
    }
    /**
     * 删除产品
     * Enter description here ...
     * @param unknown_type $where
     */
    public function removeGoods ($where)
    {
        return $this->delete($this->_order_info, $where);
    }
    /**
     * 获取订单中某个产品信息
     * Enter description here ...
     * @param unknown_type $where
     */
    public function getOrderInfo ($where)
    {
        return $this->getOne($this->_order_info, $where);
    }
    /**
     * 统计订单格式
     */
    public function countOrder ($where)
    {
        $where = $this->batchWhere($where);
        $sql = "SELECT count(id) num FROM $this->_order_list WHERE $where";
        return $this->fetch($sql);
    }
    /**
     * 返回OrderModel
     * @return OrderModel
     */
    public static function instance ()
    {
        return parent::_instance(__CLASS__);
    }
}